home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Tetris Light 1.0 / source / pref_file.c < prev    next >
Text File  |  1993-07-18  |  6KB  |  194 lines

  1. /**********************************************************************\
  2.  
  3. File:        pref_file.c
  4.  
  5. Purpose:    Contains routines to open/create preference files in the
  6.             standard loction for preference files.
  7.             
  8.  
  9. ``Tetris Light'' - a simple implementation of a Tetris game.
  10. Copyright (C) 1993 Hoylen Sue
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; see the file COPYING.  If not, write to the
  24. Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "local.h"
  29.  
  30. #include <Folders.h>
  31. #include <GestaltEqu.h>
  32.  
  33. #include "env.h"
  34. #include "pref_file.h"
  35.  
  36. /*--------------------------------------------------------------------*/
  37.  
  38. static OSErr pref_open_FindFolder(INTEGER *ref, unsigned char *name,
  39.                                   OSType creator, OSType type, Boolean create)
  40. /* Implementation of `pref_open' using the ` FindFolder' call to find the
  41.    location for the preference file. */
  42. {
  43.     register OSErr erc;
  44.     INTEGER pref_vref;
  45.     LONGINT pref_dir_id;
  46.     
  47.     /* Find the preferences directory */
  48.     
  49.     erc = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  50.                         &pref_vref, &pref_dir_id);
  51.     if (erc != noErr)
  52.         return erc;
  53.     
  54.     /* Try to open an already existing preferences file */
  55.     
  56.     *ref = HOpenResFile(pref_vref, pref_dir_id, name, fsRdWrPerm);
  57.     erc = ResError();
  58.     if (erc == noErr)
  59.         return noErr; /* Success */
  60.     
  61.     /* Failed to open */
  62.     
  63.     if ((erc == fnfErr) && create) {    
  64.         /* Try to create it if it did not exist */
  65.         
  66.         /* Create data fork, so we get its signature is correct. It may
  67.            already exist (and only the resource fork is missign), so we
  68.            ignore any errors from this call. */
  69.         
  70.         (void) HCreate(pref_vref, pref_dir_id, name, creator, type);
  71.         
  72.         /* Create the resource fork */
  73.         
  74.         HCreateResFile(pref_vref, pref_dir_id, name);
  75.         erc = ResError();
  76.         if (erc != noErr)
  77.             return erc;
  78.         
  79.         *ref = HOpenResFile(pref_vref, pref_dir_id, name, fsRdWrPerm);
  80.         erc = ResError();
  81.     }
  82.     
  83.     return erc;
  84. }
  85.  
  86. /*--------------------------------------------------------------------*/
  87.  
  88. static OSErr pref_open_SysEnvirons(INTEGER *ref, unsigned char *name,
  89.                                    OSType creator, OSType type, Boolean create)
  90. /* Implementation of `pref_open' using the ` SysEnvirons' call to find the
  91.    location for the System Folder, and uses that as the location of the
  92.    preference file. */
  93. {
  94.     register OSErr erc;
  95.     SysEnvRec env_rec;
  96.     Str255 volume_name;
  97.     INTEGER volume_ref;
  98.  
  99.     /* Save the current volume/working directory */
  100.         
  101.     erc = GetVol(volume_name, &volume_ref);
  102.     if (erc != noErr)
  103.         return erc;
  104.  
  105.     /* Get System Folder's reference number */
  106.     
  107.     erc = SysEnvirons(1, &env_rec);    /* Hope there's compiler glue for 
  108.                                        machines without this call ??? */
  109.     switch (erc) {
  110.     case noErr:
  111.         break;
  112.     case envNotPresent:
  113.         /* SysEnvirons trap not present */
  114.         env_rec.sysVRefNum = volume_ref; /* Put it on current volume */
  115.         break;
  116.     default:
  117.         return erc;
  118.         break;
  119.     }
  120.     
  121.     /* Change volumes/working directory to where the preference file goes */
  122.     
  123.     erc = SetVol(NIL, env_rec.sysVRefNum);
  124.     if (erc != noErr)
  125.         return erc;
  126.     
  127.     /* Try to open it */
  128.     
  129.     *ref = OpenResFile(name);
  130.     erc = ResError();
  131.     if (erc == noErr) {
  132.         (void) SetVol(volume_name, volume_ref);
  133.         return noErr; /* Success */
  134.     }
  135.     
  136.     if ((erc == fnfErr) && create) {
  137.         /* Try to create it */
  138.         
  139.         /* Create data fork, so we get its signature is correct. It may
  140.            already exist (and only the resource fork is missign), so we
  141.            ignore any errors from this call. */
  142.         
  143.         (void) Create(name, env_rec.sysVRefNum, creator, type);
  144.         
  145.         /* Create the resource fork */
  146.         
  147.         CreateResFile(name);
  148.         erc = ResError();
  149.         if (erc != noErr) {
  150.             (void) SetVol(volume_name, volume_ref);
  151.             return erc;
  152.         }
  153.         
  154.         *ref = OpenResFile(name);
  155.         erc = ResError();
  156.     }
  157.     
  158.     (void) SetVol(volume_name, volume_ref);
  159.     return erc;
  160. }
  161.  
  162. /*--------------------------------------------------------------------*/
  163.  
  164. OSErr pref_open(INTEGER *ref, INTEGER pref_name_str_resid,
  165.                 OSType creator, OSType type, Boolean create)
  166. /* Tries to open the perference file with the name given in the resource
  167.    string with ID `pref_name_str_resid'.  If it cannot be found, and
  168.    `create' is TRUE, it tries to create it.  The preference
  169.    file is created with the given `creator' signature.  Thpreference
  170.    file is opened as a resource file.  The opened reference is returned
  171.    in `ref'.  If an error occured, an error code is returned and `ref'
  172.    is undefined.  The name of the resource file comes from a resource
  173.    string to simplify localization. */
  174. {
  175.     register OSErr erc;
  176.     StringHandle name_handle;
  177.     
  178.     name_handle = GetString(pref_name_str_resid);
  179.     if (!name_handle)
  180.         return resNotFound;
  181.  
  182.     HLock(name_handle);
  183.     
  184.     if (env_FindFolder_available()) 
  185.         erc = pref_open_FindFolder(ref, *name_handle, creator, type, create);
  186.      else 
  187.         erc = pref_open_SysEnvirons(ref, *name_handle, creator, type, create);
  188.     
  189.     HUnlock(name_handle);
  190.     return erc;
  191. }
  192.  
  193. /*--------------------------------------------------------------------*/
  194.